package com.systop.asset.controller;

import com.github.pagehelper.PageInfo;
import com.systop.asset.entity.AssetAssignments;
import com.systop.asset.entity.Process;
import com.systop.asset.entity.RestResponse;
import com.systop.asset.entity.vo.ProcessVo;
import com.systop.asset.service.IProcessService;
import com.systop.asset.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 审批功能Controller
 * 
 * @author mu
 * @date 2025-06-24
 */
@RestController
@RequestMapping("/asset/process")
public class ProcessController {
    @Autowired
    private IProcessService processService;

    /**
     * 查询员工申请列表
     * @param vo
     * @return
     */
    @GetMapping("/empProcessList")
    public  RestResponse getEmpProcessList(ProcessVo vo){
        return RestResponse.success(processService.getEmpProcessList(vo));
    }
    /**
     * 发起申请：
     * templateId：申请模板id:1 资产借用申请 2 耗材领用申请 3 资产保修申请 4 资产归还申请
     * userId： 申请人id
     * deptId：申请人部门id
     * businessId：申请业务id、
     * @return
     */
    @GetMapping("/publishProcess")
    public RestResponse publishProcess(ProcessVo vo){
        //获取登录的用户id
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        System.out.println("当前登录的用户ID："+credentials);
        //assetAssignments.setOperatorId(Long.parseLong(credentials.toString()));
        //assetAssignments.setOperatorId(assetAssignments.getUserId());
        //发起申请
        processService.publishProcess(vo);
        return RestResponse.success();
    }

    /**
     * 流程审批
     * @param vo 审批vo对象
     * @return
     */
    @PostMapping("approve")
    public RestResponse approveProcess(@RequestBody ProcessVo vo){
        //获取登录的用户id
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        vo.setUserId(Long.parseLong(credentials.toString()));
        //TODO 调用业务层审核
        processService.approveProcess(vo);

        return RestResponse.success();
    }

    /**
     * 查询待处理
     * @param vo  查询条件 1待处理 2已处理 3我发起
     * @return
     */
    @GetMapping("findPending")
    public RestResponse findPendingTaskList(ProcessVo vo){
        //获取登录的用户id
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        System.out.println("当前登录的用户ID："+credentials);
        vo.setUserId(Long.parseLong(credentials.toString()));

        if (vo.getType()==null || vo.getType()==1){
            //查询待处理
            vo =  processService.findPendingTaskList(vo);
        }else if (vo.getType()==2){
            //查询已处理
            vo =  processService.findProcessedTaskList(vo);
        }else if (vo.getType()==3){
            //我发起
            vo =  processService.findInitiateTaskList(vo);
        }

        return RestResponse.success(vo);
    }

    /**
     * 撤销审核
     * @param processId 审核id
     * @param userId 用户id
     * @return
     */
    @GetMapping("revoke")
    public RestResponse revokeProcess(Long processId,Long userId){
        //获取登录的用户id
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        System.out.println("当前登录的用户ID："+credentials);
        //撤销审核
        processService.revokeProcess(processId,Long.parseLong(credentials.toString()));
        return RestResponse.success();
    }


    /**
     * 查询审批列表列表
     */
    //@PreAuthorize("@ss.hasPermi('asset:process:list')")
    @GetMapping("/list")
    public RestResponse list(Process process)
    {
        List<Process> list = processService.selectProcessList(process);
        return RestResponse.success(list);
    }


    /**
     * 获取审批列表详细信息
     */
    //@PreAuthorize("@ss.hasPermi('asset:process:query')")
    @GetMapping(value = "/{id}")
    public RestResponse getInfo(@PathVariable("id") Long id)
    {
        return RestResponse.success(processService.selectProcessById(id));
    }

    /**
     * 新增审批列表
     */
    //@PreAuthorize("@ss.hasPermi('asset:process:add')")
    @PostMapping
    public RestResponse add(@RequestBody Process process)
    {
        return RestResponse.success(processService.insertProcess(process));
    }

    /**
     * 修改审批列表
     */
    //@PreAuthorize("@ss.hasPermi('asset:process:edit')")
    @PutMapping
    public RestResponse edit(@RequestBody Process process)
    {
        return RestResponse.success(processService.updateProcess(process));
    }

    /**
     * 删除审批列表
     */
    //@PreAuthorize("@ss.hasPermi('asset:process:remove')")
	@DeleteMapping("/{ids}")
    public RestResponse remove(@PathVariable Long[] ids)
    {
        return RestResponse.success(processService.deleteProcessByIds(ids));
    }
}
